#!/usr/bin/env python3
"""
简化SVG生成器 - 用于K713对比
"""

def generate_simple_svg(processed_data, title="PLOOP Profile"):
    """生成简化的SVG，用于对比分析"""
    
    vertices = processed_data['processed_vertices']
    height = processed_data['height']
    
    if not vertices:
        return "<svg></svg>"
    
    # SVG画布尺寸
    canvas_width = 1000
    canvas_height = 800
    margin = 60
    
    # 计算数据边界
    x_coords = [v['x'] for v in vertices]
    y_coords = [v['y'] for v in vertices]
    
    min_x, max_x = min(x_coords), max(x_coords)
    min_y, max_y = min(y_coords), max(y_coords)
    
    # 计算绘图区域
    plot_width = canvas_width - 2 * margin - 300  # 为坐标表留空间
    plot_height = canvas_height - 2 * margin - 80  # 为标题和轴标签留空间
    
    # 坐标变换
    x_range = max_x - min_x
    y_range = max_y - min_y
    
    if x_range == 0: x_range = 1
    if y_range == 0: y_range = 1
    
    def transform_x(x):
        return margin + (x - min_x) / x_range * plot_width
    
    def transform_y(y):
        return canvas_height - margin - 40 - (y - min_y) / y_range * plot_height
    
    # 开始生成SVG
    svg_content = f'''<svg width="{canvas_width}" height="{canvas_height}" viewBox="0 0 {canvas_width} {canvas_height}" xmlns="http://www.w3.org/2000/svg">
<rect x="0" y="0" width="{canvas_width}" height="{canvas_height}" fill="white" stroke="none"/>
<text x="{canvas_width//2}" y="25" text-anchor="middle" font-family="Arial" font-size="16" fill="black">{title} (高度: {height}mm)</text>'''
    
    # 生成主路径
    if vertices:
        path_d = "M "
        for i, vertex in enumerate(vertices):
            x, y = transform_x(vertex['x']), transform_y(vertex['y'])
            if i == 0:
                path_d += f"{x:.2f} {y:.2f}"
            else:
                path_d += f" L {x:.2f} {y:.2f}"
        
        # 确保路径闭合 - 如果最后一个顶点不是起点，连接到起点
        if len(vertices) >= 2:
            first_vertex = vertices[0]
            last_vertex = vertices[-1]
            if (first_vertex['x'] != last_vertex['x'] or first_vertex['y'] != last_vertex['y']):
                x, y = transform_x(first_vertex['x']), transform_y(first_vertex['y'])
                path_d += f" L {x:.2f} {y:.2f}"
        
        svg_content += f'\n<path d="{path_d}" fill="none" stroke="blue" stroke-width="2"/>'
        
        # 生成顶点标记
        for i, vertex in enumerate(vertices):
            x, y = transform_x(vertex['x']), transform_y(vertex['y'])
            svg_content += f'\n<circle cx="{x:.0f}" cy="{y:.0f}" r="3" fill="red" stroke="white" stroke-width="1"/>'
            svg_content += f'\n<text x="{x+8:.0f}" y="{y-8:.0f}" font-family="Arial" font-size="10" font-weight="bold" fill="black">{i}</text>'
    
    # 生成网格线
    for i in range(9):
        x = margin + i * plot_width / 8
        svg_content += f'\n<line x1="{x:.0f}" y1="{canvas_height - margin - 40}" x2="{x:.0f}" y2="{margin + 40}" stroke="lightgray" stroke-width="1" opacity="0.3"/>'
    
    for i in range(8):
        y = margin + 40 + i * plot_height / 7
        svg_content += f'\n<line x1="{margin}" y1="{y:.0f}" x2="{margin + plot_width}" y2="{y:.0f}" stroke="lightgray" stroke-width="1" opacity="0.3"/>'
    
    # 生成顶点坐标表
    table_x = 710
    table_y = 690
    table_width = 280
    table_height = 100
    
    svg_content += f'\n<rect x="{table_x}" y="{table_y}" width="{table_width}" height="{table_height}" fill="white" stroke="gray" stroke-width="1" opacity="0.95" rx="5"/>'
    svg_content += f'\n<text x="{table_x + table_width//2}" y="{table_y + 15}" text-anchor="middle" font-family="Arial" font-size="12" font-weight="bold" fill="black">顶点坐标表</text>'
    
    # 表头
    svg_content += f'\n<text x="{table_x + 10}" y="{table_y + 30}" font-family="Arial" font-size="10" font-weight="bold" fill="gray">序号</text>'
    svg_content += f'\n<text x="{table_x + 50}" y="{table_y + 30}" font-family="Arial" font-size="10" font-weight="bold" fill="gray">X (mm)</text>'
    svg_content += f'\n<text x="{table_x + 150}" y="{table_y + 30}" font-family="Arial" font-size="10" font-weight="bold" fill="gray">Y (mm)</text>'
    svg_content += f'\n<line x1="{table_x + 5}" y1="{table_y + 33}" x2="{table_x + table_width - 5}" y2="{table_y + 33}" stroke="lightgray" stroke-width="1"/>'
    
    # 顶点数据
    for i, vertex in enumerate(vertices[:5]):  # 只显示前5个顶点
        y_pos = table_y + 45 + i * 12
        svg_content += f'\n<text x="{table_x + 15}" y="{y_pos}" text-anchor="middle" font-family="Arial" font-size="9" fill="black">{i}</text>'
        svg_content += f'\n<text x="{table_x + 55}" y="{y_pos}" font-family="Arial" font-size="9" fill="black">{vertex["x"]:.2f}</text>'
        svg_content += f'\n<text x="{table_x + 155}" y="{y_pos}" font-family="Arial" font-size="9" fill="black">{vertex["y"]:.2f}</text>'
    
    svg_content += '\n</svg>\n'
    
    return svg_content 